import React, { useCallback, useState } from 'react';
import { SettingFilled, SettingOutlined, UserOutlined } from '@ant-design/icons';
import { Avatar, Menu, Spin } from 'antd';
import { history, useModel } from 'umi';
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less';

/**
 * 退出登录，并且将当前的 url 保存
 */

const AvatarDropdown = ({ menu }) => {
  const { initialState, setInitialState } = useModel('@@initialState');
  const [ro, setro] = useState(false);
  const onMenuClick = useCallback(
    (event) => {
      const { key } = event;
      history.push(`/account`);
    },
    [setInitialState],
  );
  const loading = (
    <span className={`${styles.action} ${styles.account}`}>
      <Spin
        size="small"
        style={{
          marginLeft: 8,
          marginRight: 8,
        }}
      />
    </span>
  );

  if (!initialState) {
    return loading;
  }

  const { currentUser } = initialState;


  if (!currentUser || !currentUser.name) {
    return loading;
  }

  const menuHeaderDropdown = (
    <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
      <Menu.Item key="center">
        <UserOutlined />
        个人中心
      </Menu.Item>
    </Menu>
  );
  return (
    <HeaderDropdown overlay={menuHeaderDropdown}>
      <span
        className={`${styles.action} ${styles.account}`}
        onMouseOver={() => {
          setro(true);
        }}
        onMouseLeave={() => {
          setro(false);
        }}
      >
        <Avatar size="small" style={{ backgroundColor: '#5288bd' }} icon={<UserOutlined />} />
        <span className={`${styles.name} anticon`} style={{ padding: '0 8px' }}>
          {currentUser.name}
        </span>
        <SettingFilled className={styles.name} style={{ color: '#fff' }} spin={ro}></SettingFilled>
      </span>
    </HeaderDropdown>
  );
};

export default AvatarDropdown;
